package com.itheima.controller;

import com.itheima.service.*;
import com.itheima.service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/*
反射获取当前访问路径
 */
public class BaseServlet extends HttpServlet {
    public CompanyService companyService;
    public DeptService deptService;
    public CourseService courseService;
    public CatalogService catalogService;
    public QuestionService questionService;
    public UserService userService;
    public QuestionItemService questionItemService;
    public void init() throws ServletException {
        companyService=new CompanyServiceImpl();
        deptService=new DeptServiceImpl();
        courseService=new CourseServiceImpl();
        catalogService=new CatalogServiceImpl();
        questionService=new QuestionServiceImpl();
        userService=new UserServiceImpl();
        questionItemService=new QuestionItemServiceImpl();
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        try {
            String methodName = uri.substring(uri.lastIndexOf("/") + 1);
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
